package Controller;

import Service.UserService;
import Utils.JsonResult;
import Utils.MD5Tool;
import Utils.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class UserController
{
    @Autowired
    private UserService userService;
    @ResponseBody
    @RequestMapping(value = "/API/User/Register",method = RequestMethod.POST)
    public JsonResult Register(@RequestBody String params)
    {
        //创建Json返回对象
        JsonResult jsonResult = null;
        jsonResult = new JsonResult();
        try{
            //解析前端请求Json
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String,String> params_map = objectMapper.readValue(params,Map.class);
            String UserName = params_map.get("UserName");
            String PassWord = params_map.get("PassWord");
            String encrypt_PassWord = MD5Tool.encrypt(PassWord);
            User user_exit = userService.Get_UserByUserName(UserName);
            if(user_exit != null)
            {
                jsonResult.setErrorCode("该用户已存在");
            }
            else {
                Boolean status = userService.Add_User(new User(UserName,encrypt_PassWord,0));
                if(!status)
                {
                    throw new Exception();
                }
                jsonResult.setErrorCode("注册成功");
            }
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生异常，请联系管理员处理");
        }
        finally {
            return jsonResult;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/API/User/Login",method = RequestMethod.POST)
    public JsonResult Login(@RequestBody String params, HttpSession session)
    {
        //创建Json返回对象
        JsonResult jsonResult = null;
        jsonResult = new JsonResult();
        try{
            //解析前端请求Json
            ObjectMapper objectMapper = new ObjectMapper();
            System.out.println(params);
            Map<String,String> params_map = objectMapper.readValue(params,Map.class);
            String UserName = params_map.get("UserName");
            String PassWord = params_map.get("PassWord");
            String encrypt_PassWord = MD5Tool.encrypt(PassWord);
            User user_exit = userService.Get_UserByUserName(UserName);
            if(user_exit == null)
            {
                jsonResult.setErrorCode("该用户不存在");
            }
            else
            {
                if(encrypt_PassWord.equals(user_exit.getPassword()))
                {
                    //在Session中存入Token,Token为登陆凭证
                    String Token = String.valueOf(System.currentTimeMillis());
                    Token = MD5Tool.encrypt(Token);
                    session.setAttribute("Token",Token);
                    //登陆后在Session中存放UserId
                    session.setAttribute("UserId",user_exit.getUserID());
                    jsonResult.setErrorCode("登陆成功");
                }
                else
                {
                    jsonResult.setErrorCode("密码错误");
                }
            }
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生异常，请联系管理员处理");
        }
        finally {
            return jsonResult;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/API/User/ChangePw",method = RequestMethod.POST)
    public JsonResult ChangePw(@RequestBody String params,HttpSession session)
    {
        JsonResult jsonResult = null;
        jsonResult = new JsonResult();
        try{
            //解析前端请求Json
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String,String> params_map = objectMapper.readValue(params,Map.class);
            String PassWord = params_map.get("PassWord");
            String OldPassWord = params_map.get("OldPassWord");
            String UserId = (String) session.getAttribute("UserId");
            String encrypt_PassWord = MD5Tool.encrypt(PassWord);

            User user = userService.Get_UserByUserId(UserId);
            if(!OldPassWord.equals(user.getPassword()))
            {
                jsonResult.setErrorCode("旧密码错误，请重新输入");
                return jsonResult;
            }
            Boolean status = userService.Change_Password(UserId,encrypt_PassWord);
            if(!status)
            {
                jsonResult.setErrorCode("发生异常，请联系管理员处理");
                return jsonResult;
            }
            jsonResult.setErrorCode("修改密码成功");
        }
        catch (Exception e)
        {
            jsonResult.setErrorCode("发生异常，请联系管理员处理");
        }
        finally {
            return jsonResult;
        }
    }
}
